package com.dq.controller.op.opmerchant;

import com.alibaba.fastjson.JSONObject;
import com.dq.cache.J2CacheUtils;
import com.dq.exception.customException.AdminException;
import com.dq.vo.ListVo;
import com.dq.vo.Vo;
import com.dq.vo.baseVo.VO;
import com.dq.domain.sys.macro.SysMacro;
import com.dq.service.AllService;
import com.dq.utils.BeanNullUtil;
import com.dq.utils.TreeBuilder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author heshuaijun
 * @date 2019/9/1711:39
 */
@Api(description = "后台分类管理")
@RestController
@RequestMapping("/sys/macro")
public class MacroController extends AllService {
    @ApiOperation("所有单位列表")
    @GetMapping("/list")
    public ListVo<SysMacro.Vo> list() {
        //查询列表数据
        List<SysMacro> sysMacros = sysMacroRepository.findByParent((SysMacro) J2CacheUtils.get("NullSysMacro"));
        return new ListVo((List<SysMacro.Vo>) VO.toVOCollection(sysMacros), sysMacros.size());
    }

    @ApiOperation("所有单位树形列表")
    @PostMapping("/listTreeMacro")
    public Vo listTreeMacro() {
        List<SysMacro> macros = sysMacroRepository.findByDeletedIsFalseOrderByOrderNumAsc();
        List<SysMacro.Vo> sysMacroVo = (List<SysMacro.Vo>) VO.toVOCollection
                (macros);
        //TODO 优化速度
        List<SysMacro.Vo> sysMacroTree = TreeBuilder.buildMacroTree(sysMacroVo);

        JSONObject json = new JSONObject();
        json.put("macroTree", sysMacroTree);
        json.put("macro", sysMacroVo);
        return new Vo(json);
    }

    @ApiOperation("新增单位")
    @PostMapping("/add/{parentId}")
    @RequiresPermissions("sys:macro:add")
    public VO add(@RequestBody SysMacro addSysMacro, @PathVariable @ApiParam("上级id") Long parentId) throws
            AdminException {
        SysMacro sysMacro = dao.get(SysMacro.class, parentId);
        if (null == sysMacro) {
            throw new AdminException("上级单位不存在");
        }
        addSysMacro.setParent(sysMacro);
        addSysMacro.setCreateUser(shiroService.getUser());
        sysMacroRepository.save(addSysMacro);
        return new Vo(HttpStatus.OK);
    }

    @ApiOperation("编辑分类")
    @PostMapping("/update/{macroId}")
    @RequiresPermissions("sys:macro:update")
    public VO update(@RequestBody SysMacro updateMacro, @PathVariable @ApiParam(value = "单位id") Long macroId)
            throws AdminException {
        SysMacro sysMacro = dao.get(SysMacro.class, macroId);
        if (sysMacro == null) {
            throw new AdminException("未找到此单位");
        }
        BeanUtils.copyProperties(updateMacro, sysMacro, BeanNullUtil.getNullPropertyNames(updateMacro));
        sysMacroRepository.save(sysMacro);
        return new Vo(HttpStatus.OK);
    }


    @ApiOperation("删除分类")
    @PostMapping("/delete/{macroId}")
    @RequiresPermissions("sys:macro:delete")
    public VO delete(@PathVariable @ApiParam(value = "单位id") Long macroId) throws AdminException {
        SysMacro sysMacro = dao.get(SysMacro.class, macroId);
        if (sysMacro == null) {
            throw new AdminException("未找到此单位");
        }
        sysMacro.setDeleted(true);
        sysMacroRepository.save(sysMacro);
        return new Vo(HttpStatus.OK);
    }
}
